package cc.fine.svc.main.controller;

import cc.fine.svc.common.utils.R;
import cc.fine.svc.main.bean.FineMessage;
import cc.fine.svc.main.bean.TagWeight;
import cc.fine.svc.main.bean.RecommendationTypeVo;
import cc.fine.svc.main.bean.vo.VideoViewVo;
import cc.fine.svc.main.service.WeightService;
import cc.fine.svc.main.service.recommendation.RecommendationService;
import cc.fine.svc.main.service.tbl.TblUserService;
import cc.fine.svc.main.service.tbl.TblVideoService;
import cc.fine.svc.main.utils.MqUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @AUTHOR: wyihe
 * @DATE: 2023/10/30
 */
@RestController
@RequestMapping("/view/video")
@Api(description = "视频观看相关接口")
public class ViewController {

    @Autowired
    private TblUserService userService;

    @Autowired
    private TblVideoService videoService;

    @Autowired
    private MqUtils mqUtils;

    @Autowired
    private WeightService weightService;

    @Autowired
    private RecommendationService recommendationService;

    @ApiOperation("展示视频信息（包含发布者、用户是否收藏、是否点赞、收藏夹集合这些信息）")
    @GetMapping("/{videoId}/{userId}")
    public R view(@PathVariable String videoId,
                  @PathVariable String userId) {

        VideoViewVo vo = videoService.getVideoViewVo(videoId, userId);


        return R.ok().data("data", vo);
    }

    @ApiOperation("处理权重 (传 tagNames )")
    @PostMapping("/weight/")
    public R weight(@RequestBody TagWeight weight) {
        mqUtils.send(new FineMessage(FineMessage.Weight, weight));

        return R.ok();
    }

    @ApiOperation("推荐视频（不用登录，推荐最热门的）, page 从 0 开始")
    @PostMapping("/recommendation/no-login/")
    public R recommendationNoLogin(Integer page, Integer limit) {
        List<Map<String, Object>> recommendation = recommendationService.recommendationNoLogin(page, limit);

        return R.ok().data("recommendation", recommendation);
    }

    @ApiOperation("向用户推荐n条视频, tags 传 标签名")
    @PostMapping("/recommendation/{userId}/{n}")
    public R recommendation(@PathVariable String userId,
                            @PathVariable Integer n,
                            @RequestBody(required = false) RecommendationTypeVo vo) {
        List<Map<String, Object>> recommendation = null;

        if (vo.getType().equals("auto") || StringUtils.isEmpty(vo.getType()))
            recommendation = recommendationService.recommendation(userId, n);

        if (vo.getType().equals("ip"))
            recommendation = recommendationService.recommendationForIp(vo.getIp(), userId, n);

        if (vo.getType().equals("tag"))
            recommendation = recommendationService.recommendationForTag(vo.getTags(), userId, n);


        if (recommendation == null) {
            return R.error().message("传入的参数不合法");
        }

        return R.ok().data("recommendation", recommendation);
    }

    @ApiOperation("fan获取视频列表, page从0开始")
    @GetMapping("/fan/{userId}")
    public R fan(@PathVariable String userId,
                 Integer page, Integer limit) {
        List<VideoViewVo> list = videoService.fanVideos(userId, page, limit);

        return R.ok().data("data", list);
    }
}
